﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class RelatedWordsServiceTextPage : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        //Default functionality.
        if (!textTextBox.Text.Equals(""))
        {
            textCheckbox.Disabled = false;
            textCheckbox.Checked = true;
            textTextBox.Enabled = true;
            urlCheckbox.Disabled = true;
            urlTextBox.Enabled = false;
            urlCheckbox.Checked = false;
        }
        else
        {
            textCheckbox.Disabled = true;
            textCheckbox.Checked = false;
            textTextBox.Enabled = false;
            urlCheckbox.Disabled = false;
            urlTextBox.Enabled = true;
            urlCheckbox.Checked = true;
        }
    }

    protected void Submit_Click(object sender, EventArgs e)
    {
        string wordToFind = wordToFindTextBox.Text;
        if (wordToFind.Split().Length > 1)
        {
            this.output.InnerHtml = "<font color=\"red\"><b>INCORRECT INPUT: Please only search one word.</b></font>";
            return;
        }
        RelatedWordsService.RelatedWordsWCFServiceClient client = new RelatedWordsService.RelatedWordsWCFServiceClient();
        string[] relatedWords = null;
        if (textCheckbox.Checked)
        {
            relatedWords = client.getRelatedWords(wordToFind, textTextBox.Text.Trim());
            client.Close();
        }
        else
        {
            try
            {
                relatedWords = client.getRelatedWordsFromAddress(wordToFind, new Uri(urlTextBox.Text));
                client.Close();
            }
            catch (Exception ex)
            {
            }
        }
        if (relatedWords != null && relatedWords.Length != 0)
        {
            string words = "";
            foreach (string word in relatedWords)
            {
                words += word;
                words += ", ";
            }
            this.output.InnerText = "Output: " + words;
        }
        else
        {
            this.output.InnerText = "No Output :-(";
        }
    }
}